package com.example.ywj.myapplication.handler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
import com.example.ywj.myapplication.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HandlerActivity extends AppCompatActivity {
    private Button btn_download1,btn_download2;
    private ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);
        btn_download1 =findViewById(R.id.btn_download1);
        btn_download2 =findViewById(R.id.btn_download2);
        iv =findViewById(R.id.imageView);
        btn_download1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                download1();
            }
        });
        btn_download2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                download2();
            }
        });
    }
    /**
     * 主线程
     */
    Handler handler = new Handler(){
        /**
         * handleMessage用户处理消息
         * @param msg
         */
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case  100:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    // 更新ImageView操作
                    iv.setImageBitmap(bitmap);
                    break;
            }
        }
    };
    public void download2(){
        new Thread(){
            @Override
            public void run() {
                // HttpURLconnection方式获取图片流
                //InputStream is = null;

                // 将流转换成Bitmap对象
                Bitmap bitmap = get();

                // handler发送消息
                Message msg = Message.obtain();
                //  发送给主线程的标记
                msg.what = 100;
                // 发送给主线程的对象
                msg.obj  = bitmap;
                // 发送消息
                handler.sendMessage(msg);
            }
        }.start();

    }
    private void download1(){
        // 开启线程下载图片
        new Thread(){
            @Override
            public void run() {
                //  模拟下载图片
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 从服务端去拿流，然后更新ImageView,所以需要想办法回到主线程？
                // 方式1：使用runOnUiThread,回到主线程
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        iv.setImageResource(R.drawable.xz1);
                    }
                });

            }
        }.start();
    }
    private  Bitmap  get(){
        Bitmap bitmap = null;

        try {
            // 1.创建URL对象
            URL url = new URL("http://10.11.71.87:8080/images/xiaoji.jpg");
            // 2.获得连接
            HttpURLConnection conn  = (HttpURLConnection) url.openConnection();
            // 3. 设置请求方式(如果不设置，默认get方式)
            conn.setRequestMethod("GET");
            // 4.设置超时时间
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            //  5.连接
            conn.connect();
            int responseCode =  conn.getResponseCode();
            // 返回200时，成功获取数据
            if(responseCode == HttpURLConnection.HTTP_OK){
                InputStream is =  conn.getInputStream();
                // 将流转为bitmap对象   (  也可以将流is转为String)
                bitmap = BitmapFactory.decodeStream(is);

            }
        } catch (Exception e) {
            //Log.e(TAG, "get: "+e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }
}
